package org.micode.demo.biz.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.micode.common.helper.I18nMsgHelper;
import org.micode.common.utils.ObjectUtils;
import org.micode.demo.biz.service.BizSysDictionaryService;
import org.micode.demo.db.cache.config.SysConfigCache;
import org.micode.demo.db.cache.dictionary.ISysDictionaryCacheHandler;
import org.micode.demo.db.cache.dictionary.SysDictionaryCache;
import org.micode.demo.db.cache.user.SysUserCache;
import org.micode.demo.db.domain.dto.SysConfigDTO;
import org.micode.demo.db.domain.dto.SysDictionaryDTO;
import org.micode.demo.db.service.DaoSysDictionaryService;
import org.micode.libs.cache.imagecode.ImageCodeCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户服务，登录、保存个人信息等
 */
@Service
@Slf4j
public class BizSysDictionaryServiceImpl implements BizSysDictionaryService {

    @Autowired
    private DaoSysDictionaryService daoSysDictionaryService;

    @Autowired
    private ISysDictionaryCacheHandler sysDictionaryCacheHandler;

    @Override
    public SysDictionaryDTO getValueByKey(String type, String key) {
        SysDictionaryCache dictCache = sysDictionaryCacheHandler.getByKey(SysDictionaryCache.dictKey(type));
        if (null == dictCache) {
            log.warn(
                    I18nMsgHelper.get("warn.BizSysDictionaryServiceImpl-getValueByKey.none-fail") + "(type={},key={})",
                    type, key);

        } else {
            try {
                return dictCache.getDictItems().get(key);
            } catch (Exception e) {
                log.warn(
                        I18nMsgHelper.get("warn.BizSysDictionaryServiceImpl-getValueByKey.get-value-fail")
                                + ": type={},key={}",
                        type, key);
            }
        }
        return null;
    }

    @Override
    public List<SysDictionaryDTO> listByType(String type) {
        SysDictionaryCache dictCache = sysDictionaryCacheHandler.getByKey(SysDictionaryCache.dictKey(type));
        if (null == dictCache) {
            log.warn(
                    I18nMsgHelper.get("warn.BizSysDictionaryServiceImpl-getValueByKey.none-fail") + "(type={})",
                    type);
        } else {
            try {
                return new ArrayList<>(dictCache.getDictItems().values());
            } catch (Exception e) {
                log.warn(
                        I18nMsgHelper.get("warn.BizSysDictionaryServiceImpl-getValueByKey.get-value-fail")
                                + ": type={}",
                        type);
            }
        }
        return null;
    }

    @Override
    public void loadSysDictionaryCache() {
        List<SysDictionaryDTO> list = daoSysDictionaryService.loadAllList();
        if (null != list) {
            Map<String, SysDictionaryCache> cacheMap = new HashMap<>();
            list.forEach(item -> {
                SysDictionaryCache sysDict = cacheMap.get(item.getParamType());
                if (null == sysDict) {
                    sysDict = new SysDictionaryCache();
                }
                ObjectUtils.copyProperties(item, sysDict);
                sysDict.getDictItems().put(item.getParamKey(), item);
                cacheMap.put(item.getParamType(), sysDict);
            });

            cacheMap.forEach((paramType, cacheObj) -> {
                sysDictionaryCacheHandler.setByKey(
                        SysDictionaryCache.dictKey(paramType),
                        cacheObj);
            });
        }
    }

}
